package com.hengxinguotong.hxgtwygsp.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期转换工具
 */
public class DateUtil
{

    public static final String PATTERN1 = "yyyy-MM-dd";
    public static final String PATTERN2 = "yyyy-MM-dd hh:mm";
    public static final String PATTERN3 = "yyyy-MM-dd HH:mm";
    public static final String PATTERN4 = "yyyy-MM-dd hh:mm:ss";// 12小时制
    public static final String PATTERN5 = "yyyy-MM-dd HH:mm:ss";// 24小时制

    public static final String PATTERN6 = "yyy/MM/dd";
    public static final String PATTERN7 = "yyy/MM/dd hh:mm";
    public static final String PATTERN8 = "yyy/MM/dd HH:mm";
    public static final String PATTERN9 = "yyyy/MM/dd hh:mm:ss";// 12小时制
    public static final String PATTERN10 = "yyyy/MM/dd HH:mm:ss";// 24小时制

    public static final String PATTERN11 = "HH:mm";// 24小时制
    public static final String PATTERN12 = "yyyyMMdd";
    public static final String PATTERN13 = "MM-dd";
    public static final String PATTERN14 = "HH:mm:ss";

    public static final String PATTERN15 = "yyyy年MM月";
    public static final String PATTERN16 = "yyyy年MM月dd日";
    public static final String PATTERN17 = "yyyy年MM月dd日hh时mm分";
    public static final String PATTERN18 = "yyyy年MM月dd日HH时mm分";
    public static final String PATTERN19 = "yyyy年MM月dd日hh时mm分ss秒";
    public static final String PATTERN20 = "yyyy年MM月dd日HH时mm分ss秒";

    private static SimpleDateFormat sdf = null;

    /**
     * 字符串型的日期转时间戳
     *
     * @param date
     * @return
     */
    public static long stringToLong(String date, String pattern)
    {
        long datelong = 0;
        if (sdf == null)
        {
            sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
        }
        sdf.applyPattern(pattern);
        try
        {
            datelong = sdf.parse(date).getTime();
        } catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return datelong;
    }

    /**
     * 时间字符串到Date
     *
     * @param date
     * @param pattern
     * @return
     */
    public static Date stringToDate(String date, String pattern)
    {
        Date result = null;
        if (sdf == null)
        {
            sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
        }
        sdf.applyPattern(pattern);
        try
        {
            result = sdf.parse(date);
        } catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 时间戳转字符串日期
     *
     * @param datelong
     * @param pattern
     * @return
     */
    public static String longToString(long datelong, String pattern)
    {
        String date = null;
        if (sdf == null)
        {
            sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
        }
        sdf.applyPattern(pattern);
        date = sdf.format(datelong);
        return date;
    }

    /**
     * 时间戳转字符串日期
     *
     * @param dateInt
     * @param pattern
     * @return
     */
    public static String intToString(int dateInt, String pattern)
    {
        String date = null;
        if (sdf == null)
        {
            sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
        }
        sdf.applyPattern(pattern);
        date = sdf.format(dateInt * 1000L);
        return date;
    }

    /**
     * 判断是否是当天，是则显示时分，不是则显示年月日
     *
     * @param datelong
     * @return
     */
    public static String longToString(long datelong)
    {
        String date = null;
        if (sdf == null)
        {
            sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
        }
        Calendar current = Calendar.getInstance();
        current.setTime(new Date(datelong));
        Calendar today = Calendar.getInstance();
        if (current.get(Calendar.YEAR) == today.get(Calendar.YEAR)
                && current.get(Calendar.MONTH) == today.get(Calendar.MONTH)
                && current.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH))
        {
            sdf.applyPattern(PATTERN11);
        } else
        {
            sdf.applyPattern(PATTERN1);
        }
        date = sdf.format(datelong);
        return date;
    }

    /**
     * date转字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String dateToString(Date date, String pattern)
    {
        String datestring = null;
        if (sdf == null)
        {
            sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
        }
        sdf.applyPattern(pattern);
        datestring = sdf.format(date);
        return datestring;
    }

    /**
     * 获取Date中的年月日
     *
     * @param date
     * @param field
     * @return
     */
    public static int get(Date date, int field)
    {
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        return cld.get(field);
    }

    /**
     * 获取前一天
     *
     * @param date
     * @return
     */
    public static Date getPreDay(Date date)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);
        return c.getTime();
    }

    /**
     * 获取下一天
     *
     * @param date
     * @return
     */
    public static Date getNextDay(Date date)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);
        return c.getTime();
    }

    /**
     * 日期转星期几
     *
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date)
    {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if (date != null)
        {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
        {
            w = 0;
        }
        return weekOfDays[w];
    }

    /**
     * 判断两个日期是否是同一天
     *
     * @param firstDate
     * @param sencondDate
     * @return
     */
    public static boolean sameDay(Date firstDate, Date sencondDate)
    {
        Calendar first = Calendar.getInstance();
        first.setTime(firstDate);
        Calendar second = Calendar.getInstance();
        second.setTime(sencondDate);
        if (first.get(Calendar.YEAR) == second.get(Calendar.YEAR)
                && first.get(Calendar.MONTH) == second.get(Calendar.MONTH)
                && first.get(Calendar.DAY_OF_MONTH) == second.get(Calendar.DAY_OF_MONTH))
        {
            return true;
        } else
        {
            return false;
        }
    }
}
